package com.example.shopping_li.cart;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.example.shopping_li.R;
import com.example.shopping_li.URLUtils;
import com.example.shopping_li.me.UserBean;
import com.example.shopping_li.utils.BaseActivity;
import com.example.shopping_li.utils.SaveUserUtils;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class CartActivity extends BaseActivity {
ListView lv;
TextView moneyTv;
private CartAdapter adapter;
List<Boolean>statuList;
int totalMoney;
    private List<CartBean.DataBean> data;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart);
        lv=findViewById(R.id.cart_daily_lv);
        moneyTv=findViewById(R.id.cart_cal_tv);
       data=new ArrayList<>();
       statuList=new ArrayList<>();
        //适配器
        adapter=new CartAdapter(this,data,statuList);
        lv.setAdapter(adapter);

        //加载数据
        UserBean.DataBean userInfo = getUserInfo();
        int id = userInfo.getId();//用户id
        String url= URLUtils.queryCartURL+"?userId="+id;
        connectNet(url);
adapter.setSelectGoodsListener(new CartAdapter.SelectGoodsListener() {
    @Override
    public void selectGoods(boolean status, int pos) {
//        CartBean.DataBean bean=data.get(pos);
//        double totalprices = bean.getTotalprices();
//        if (status) {
//            totalMoney+=totalprices;
//            moneyTv.setText("￥"+totalMoney);
//        }else {
//            if (totalMoney>totalprices) {
//                totalMoney-=totalprices;
//                moneyTv.setText("￥");
//            }
//        }
        double total=0;
        for (int i=0;i<statuList.size();i++){
            if (statuList.get(i)){
                double v=data.get(i).getTotalprices();
                total+=v;
            }
        }
    }
});
    }

    @Override
    public void onSuccess(String result) {
        CartBean cartBean = new Gson().fromJson(result, CartBean.class);
        List<CartBean.DataBean> list = cartBean.getData();

        for (int i=0;i<list.size();i++){
            statuList.add(false);
        }
        if (list != null && list.size() > 0) {
            data.addAll(list);
            adapter.notifyDataSetChanged();
        }
    }

    private UserBean.DataBean getUserInfo() {
        UserBean.DataBean bean=null;
        bean= SaveUserUtils.getUserinfo();
        if (bean==null) {
            bean=SaveUserUtils.getUserToObj(this);
        }
        return bean;
    }
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.all_daily_back:
                finish();
                break;
            case R.id.cart_calculate_btn:
                break;
        }
    }
}
